package com.breeze.socketdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.breeze.socketdemo.tcp.TcpClient;
import com.breeze.socketdemo.tcp.TcpServer;

public class MainActivity extends AppCompatActivity {
    public static final int MSG_RECEIVE_FROM_CLIENT = 0;
    public static final int MSG_RECEIVE_FROM_SERVER = 1;
    private EditText mServerMessageET;
    private EditText mClientMessageET;
    private TcpClient mTcpClient;
    private TcpServer mTcpServer;
    private TextView mMessageServerTV;
    private TextView mMessageClientTV;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String message = (String) msg.obj;
            switch (msg.what) {
                case MSG_RECEIVE_FROM_CLIENT:
                    receiveFromClient(message);
                    break;
                case MSG_RECEIVE_FROM_SERVER:
                    receiveFromServer(message);
                    break;
            }
        }
    };

    private void receiveFromServer(String message) {
        mMessageClientTV.setText(mMessageClientTV.getText().toString() + "\n" + "server:" + message);
    }

    private void receiveFromClient(String message) {
        mMessageServerTV.setText(mMessageServerTV.getText().toString() + "\n" + "client:" + message);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initServerClient();
    }


    private void initView() {
        mServerMessageET = findViewById(R.id.et_message_server);
        mClientMessageET = findViewById(R.id.et_message_client);
        mMessageServerTV = findViewById(R.id.tv_message_server);
        mMessageClientTV = findViewById(R.id.tv_message_client);
    }

    private void initServerClient() {
        mTcpServer = new TcpServer(mHandler);
        mTcpClient = new TcpClient(mHandler);
    }

    public void click(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_start_server:
                startTCPServer();
                break;
            case R.id.btn_start_tcp_client:
                connectTCPServer();
                break;
            case R.id.btn_client_send:
                sendMsgToServer();
                break;
            case R.id.btn_server_send:
                sendMsgToClient();
                break;
        }
    }


    private void sendMsgToServer() {
        String msg = mClientMessageET.getText().toString();
        mClientMessageET.setText("");
        mTcpClient.sendMsg(msg);
    }

    private void sendMsgToClient() {
        String msg = mServerMessageET.getText().toString();
        mServerMessageET.setText("");
        mTcpServer.sendMsg(msg);
    }

    private void connectTCPServer() {
        mTcpClient.connect("10.20.0.85", 58627);
    }

    private void startTCPServer() {
        mTcpServer.start();
    }
}